package com.zfei.gfb.service;

import com.zfei.gfb.api.StatsService;
import com.zfei.gfb.api.domain.StatsValue;
import com.zfei.gfb.bean.Status;
import com.zfei.gfb.bean.StatusException;
import com.zfei.gfb.model.StatsEntity;
import com.zfei.gfb.dao.StatsMapper;
import com.zfei.gfb.util.Beans;
import com.zfei.gfb.util.Now;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class StatsServiceImpl implements StatsService {

    @Resource
    private StatsMapper statsMapper;

    @Override
    public int set(StatsValue value) throws StatusException {
        return 0;
    }

    @Override
    public int inc(StatsValue value) throws StatusException {
        int ret = 0;
        try {
            ret = statsMapper.inc(getForUpdate(value));
        } catch (StatusException e) {
            if (Status.NOT_EXIST.value() == e.getStatusCode()) {
                long now = Now.millis();
                StatsEntity entity = new StatsEntity();
                BeanUtils.copyProperties(value, entity);
                entity.setCreateTime(now);
                entity.setUpdateTime(now);
                ret = this.statsMapper.insertSelective(entity);
            }
        }
        return ret;
    }

    @Override
    public int dec(StatsValue value) throws StatusException {
        int ret = 0;
        try {
            ret = statsMapper.dec(getForUpdate(value));
        } catch (StatusException ignore) {

        }
        return ret;
    }

    @Override
    public int set(List<StatsValue> values) throws StatusException {
        return 0;
    }

    @Override
    public int inc(List<StatsValue> values) throws StatusException {

        return 0;
    }

    @Override
    public int dec(List<StatsValue> values) throws StatusException {
        return 0;
    }

    @Override
    public StatsValue get(String id, String tag) throws StatusException {
        if (StringUtils.isAnyBlank(id, tag)) {
            return new StatsValue();
        }

        StatsEntity entity = this.statsMapper.findByIdAndTag(id, tag);
        return entity != null ? Beans.copy(entity, StatsValue.class) : new StatsValue();
    }

    @Override
    public long getSumByTag(String tag) throws StatusException {
        if (StringUtils.isAnyBlank(tag)) {
            return 0L;
        }
        long sum = this.statsMapper.getSumByTag(tag);
        return sum;
    }

    @Override
    public List<StatsValue> list(List<String> ids) throws StatusException {
        return null;
    }

    @Override
    public void track(Object... data) {

    }


    public StatsEntity getForUpdate(StatsValue value) {
        StatsEntity entity = getForUpdate(value.getId(), value.getTag());
        if (entity == null) {
            throw new StatusException(Status.NOT_EXIST);
        }

        long now = Now.millis();
        entity = new StatsEntity();
        BeanUtils.copyProperties(value, entity);
        entity.setUpdateTime(now);
        return entity;
    }

    public StatsEntity getForUpdate(String id, String tag) {
        return statsMapper.findByIdAndTagForUpdate(id, tag);
    }
}
